﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DynORM.Interfaces;
using DynORM.Mappers;
using DynORM.Enums;
using DynORM;
using DynORM.ValueObjects;
using DynORM.ReferenceObjects;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace UnitTests.DAL
{
    [TestClass]
    public class FieldDescriptorSerializationUnitTests
    {
        private IFieldDescriptor GenerateNewFieldDescriptor(Type type, string name)
        {
            return new FieldDescriptor(type, name);
        }

        private string XmlXPathRunner(string xml, string xpath)
        {
            //intencially use XPath here in order to make this test independed from formatters
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            //XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            //nsmgr.AddNamespace("ns", "http://schemas.datacontract.org/2004/07/IEOServerJobWebAPI.Auxiliary");
            //nsmgr.AddNamespace("i", "http://www.w3.org/2001/XMLSchema-instance");

            //XmlNode node = doc.SelectSingleNode(xpath, nsmgr);
            XmlNode node = doc.SelectSingleNode(xpath);

            return node.InnerText;
        }

        [TestMethod]
        public void SerializeFieldDescriptorTest()
        {
            string xml;
            using (MemoryStream ms = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(ms, UTF8Encoding.Default);

                XmlSerializer formatter = new XmlSerializer(typeof(FieldDescriptor));
                IFieldDescriptor fd = GenerateNewFieldDescriptor(typeof(string), "TestColumn");
                formatter.Serialize(writer, fd);

                ms.Position = 0;
                StreamReader sr = new StreamReader(ms);
                xml = sr.ReadToEnd();
                //<?xml version="1.0" encoding="Windows-1252"?><FieldDescriptor FieldName="TestColumn" FieldType="System.String" />
            }
            string res = XmlXPathRunner(xml, "//FieldDescriptor[1]/@FieldName");
            Assert.AreEqual("TestColumn", res);
        }

        [TestMethod]
        public void DeserializeFieldDescriptorTest()
        {
            IFieldDescriptor fd;
            using (MemoryStream ms = new MemoryStream())
            {
                XmlTextWriter writer = new XmlTextWriter(ms, UTF8Encoding.Default);

                XmlSerializer formatter = new XmlSerializer(typeof(FieldDescriptor));
                fd = GenerateNewFieldDescriptor(typeof(string), "TestColumn");
                formatter.Serialize(writer, fd);

                ms.Position = 0;
                XmlTextReader reader = new XmlTextReader(ms);
                fd = (IFieldDescriptor)formatter.Deserialize(reader);
            }
            Assert.AreEqual(fd.FieldName, "TestColumn");
            Assert.AreEqual<Type>(fd.FieldType, typeof(string));
        }

    }
}
